<template>
  <div class="stock-list">
    <div class="search-bar">
      <input v-model="searchKeyword" 
             @input="handleSearch" 
             placeholder="输入股票代码或名称搜索" />
      <div v-if="searchResults.length" class="search-results">
        <div v-for="stock in searchResults" 
             :key="stock.code"
             @click="addStock(stock)"
             class="search-item">
          {{ stock.name }} ({{ stock.code }})
        </div>
      </div>
    </div>

    <table>
      <thead>
        <tr>
          <th>代码</th>
          <th>名称</th>
          <th>最新价</th>
          <th>涨跌幅</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="stock in stocks" 
            :key="stock.code"
            :class="{ 'up': stock.changePercent > 0, 'down': stock.changePercent < 0 }">
          <td>{{ stock.code }}</td>
          <td>{{ stock.name }}</td>
          <td>{{ stock.price }}</td>
          <td>{{ stock.changePercent }}%</td>
          <td>
            <button @click="confirmRemoveStock(stock.code)" class="remove-button">删除</button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useStockStore } from '../store'
import { storeToRefs } from 'pinia'
import { ElMessageBox } from 'element-plus'
import 'element-plus/dist/index.css'

const stockStore = useStockStore()
const { stocks, searchResults } = storeToRefs(stockStore)
const searchKeyword = ref('')

// 组件加载时初始化数据
onMounted(async () => {
  await stockStore.initialize()
})

const handleSearch = async () => {
  if (searchKeyword.value.length >= 2) {
    await stockStore.searchStocks(searchKeyword.value)
  }
}

const addStock = (stock) => {
  stockStore.addStock(stock)
  searchKeyword.value = ''
}

const confirmRemoveStock = (code) => {
  ElMessageBox.confirm('确定要删除这只股票吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    center: true,
    showClose: true,
    closeOnClickModal: true,
    closeOnPressEscape: true,
    modalAppendToBody: true,
    appendToBody: true,
    customClass: 'custom-message-box',
    beforeClose: (action, instance, done) => {
      if (action === 'confirm') {
        done()
      } else {
        done()
      }
    }
  }).then(() => {
    stockStore.removeStock(code)
  }).catch(() => {
    // 用户取消操作
  })
}
</script>

<style>
/* 注意：这里不使用 scoped，确保样式可以影响到全局弹窗 */
.el-message-box {
  position: fixed !important;
  top: 50% !important;
  left: 50% !important;
  transform: translate(-50%, -50%) !important;
  margin: 0 !important;
  max-width: 90% !important;
  width: auto !important;
  z-index: 2000 !important;
}

.el-overlay {
  position: fixed !important;
  top: 0 !important;
  right: 0 !important;
  bottom: 0 !important;
  left: 0 !important;
  z-index: 1999 !important;
  background-color: rgba(0, 0, 0, 0.5) !important;
  overflow: auto !important;
}

.custom-message-box {
  border-radius: 8px !important;
  padding: 20px !important;
  background: #fff !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1) !important;
}

.custom-message-box .el-message-box__header {
  padding: 15px 20px !important;
}

.custom-message-box .el-message-box__content {
  padding: 20px !important;
}

.custom-message-box .el-message-box__btns {
  padding: 10px 20px 20px !important;
}

.custom-message-box .el-button {
  padding: 8px 20px !important;
  font-size: 14px !important;
}

.custom-message-box .el-button--primary {
  background-color: #409eff !important;
  border-color: #409eff !important;
  color: #fff !important;
}

.custom-message-box .el-button--default {
  background-color: #fff !important;
  border-color: #dcdfe6 !important;
  color: #606266 !important;
}

/* 全局滚动条样式 */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}

::-webkit-scrollbar-thumb {
    background: #c1c1c1;
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: #a8a8a8;
}
</style>

<style scoped>
.stock-list {
  margin-top: 20px;
}

.search-bar {
  position: relative;
  margin-bottom: 20px;
}

input {
  width: 100%;
  padding: 8px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  z-index: 1000;
}

.search-item {
  padding: 8px;
  cursor: pointer;
}

.search-item:hover {
  background: #f5f7fa;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th, td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #dcdfe6;
}

.up {
  color: #f56c6c;
}

.down {
  color: #67c23a;
}

.remove-button {
  background-color: #ff4d4f; /* 红色背景 */
  color: white; /* 白色文字 */
  border: none; /* 无边框 */
  border-radius: 4px; /* 圆角 */
  padding: 8px 12px; /* 内边距 */
  cursor: pointer; /* 鼠标悬停时显示手型 */
  transition: background-color 0.3s; /* 背景色过渡效果 */
}

.remove-button:hover {
  background-color: #ff7875; /* 悬停时的背景色 */
}

.remove-button:active {
  background-color: #d9363e; /* 点击时的背景色 */
}
</style> 